package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    
    /**
    * @Description: 顾客新增地址
            * @Param: 
            * @return: 
            * @Author: Lei
            * @Date: 
     */
    
    @PostMapping
    public R<AddressBook> saveAddress(@RequestBody AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());

        log.info("addressbook:{}",addressBook);

        /**如果当前顾客没有地址，则首次新增地址时设置为默认地址*/
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        AddressBook addressBookOne = addressBookService.getOne(lambdaQueryWrapper);
        if(addressBookOne==null){
            addressBook.setIsDefault(1);
            addressBookService.save(addressBook);
        }else {
            addressBookService.save(addressBook);
        }
        return R.success(addressBook);
    }
    
    /**
    * @Description: 查询用户所有地址
            * @Param: 
            * @return: 
            * @Author: Lei
            * @Date: 
     */
    
    @GetMapping("/list")
    public R<List<AddressBook>> listAddressbook(AddressBook addressBook){
        addressBook.setUserId(BaseContext.getCurrentId());
        log.info("addressBook:{}",addressBook);


        /**条件构造器*/
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(addressBook.getUserId()!=null,AddressBook::getUserId,addressBook.getUserId());
        lambdaQueryWrapper.orderByDesc(AddressBook::getUpdateTime);


        return R.success(addressBookService.list(lambdaQueryWrapper));
    }

    /**
    * @Description: 设置顾客的默认地址
            * @Param: 
            * @return: 
            * @Author: Lei
            * @Date: 
     */
    
    @PutMapping("/default")
    public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
        log.info("addressbook:{}",addressBook);
        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        lambdaUpdateWrapper.set(AddressBook::getIsDefault,0);

        addressBookService.update(lambdaUpdateWrapper);

        addressBook.setIsDefault(1);

        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    /**
    * @Description: 根据id查询地址
            * @Param:
            * @return:
            * @Author: Lei
            * @Date:
     */

    @GetMapping("/{id}")
    public R getAddressBookById(@PathVariable Long id){
        AddressBook addressBook=addressBookService.getById(id);
        if(addressBook!=null){
            return R.success(addressBook);
        }else{
            return R.error("没有找到地址");
        }

    }

    /**
    * @Description: 删除顾客地址
            * @Param: 
            * @return: 
            * @Author: Lei
            * @Date: 
     */
    
    @DeleteMapping
    public R<String> deleteAddress(@RequestParam Long ids){
        log.info("ids:{}",ids);
        LambdaUpdateWrapper<AddressBook> lambdaUpdateWrapper=new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ids!=null,AddressBook::getId,ids);
        addressBookService.remove(lambdaUpdateWrapper);
        return R.success("地址删除成功");

    }


    /**
    * @Description: 顾客修改地址
            * @Param: 
            * @return: 
            * @Author: Lei
            * @Date: 
     */
    
    @PutMapping
    public R<String> updateAddressBook(@RequestBody AddressBook addressBook){
        log.info("addressbook:{}",addressBook);

        addressBookService.updateById(addressBook);

        return R.success("地址修改成功");
    }
    
    /**
    * @Description: 查看默认地址
            * @Param: 
            * @return: 
            * @Author: Lei
            * @Date: 
     */
    
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.eq(AddressBook::getIsDefault,1);

        AddressBook addressBook = addressBookService.getOne(lambdaQueryWrapper);
        if(addressBook==null){
            return R.error("没有找到默认地址");
        }else {
            return R.success(addressBook);
        }

    }
}
